﻿using ETask.Api.Models;
using Microsoft.EntityFrameworkCore;

namespace ETask.Api.Data
{
    public static class PaginationExtensions
    {
        public static async Task<PagedResult<T>> ToPagedResultAsync<T>(
            this IQueryable<T> query,
            PaginationParameters pageDto)
        {
            var totalCount = await query.CountAsync();
            var items = await query
                .Skip((pageDto.PageNumber - 1) * pageDto.PageSize)
                .Take(pageDto.PageSize)
                .ToListAsync();

            return new PagedResult<T>
            {
                Items = items,
                TotalCount = totalCount,
                TotalPages = (int)Math.Ceiling(totalCount / (double)pageDto.PageSize),
                CurrentPage = pageDto.PageNumber,
                PageSize = pageDto.PageSize
            };
        }
    }
}
